﻿using System;
using System.Windows;
using System.Windows.Data;
using AccessControl.Gateway.Managers;
using AccessControl.Contracts.Models;
using Action = AccessControl.Contracts.Models.Action;

namespace AccessControl.Wpf.Converters
{
	class IsOperatorActionAllowedToVisibilityConverter : IMultiValueConverter
	{
		#region Public Properties

		public Visibility TrueValue { get; set; }
		public Visibility FalseValue { get; set; }

		#endregion

		#region IMultiValueConverter Members

		public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (values[0] == null || values[1] == null)
				return FalseValue;

			if (values[0] is IOperator && values[1] is Action)
			{
				IOperator myOperator = (IOperator)values[0];
				Action action = (Action)values[1];

				if (myOperator == null)
					return FalseValue;

				foreach (Action allowedAction in myOperator.AllowedActions)
				{
					if (allowedAction == action)
						return TrueValue;
				}
			}

			return FalseValue;
		}

		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
